#pragma once
#include "safeque.hxx"
#include "mythread.hxx"
#include <string>
#include <vector>
#include <map>
#include <set>
#include "auto.hxx"
#include "task.hxx"

using namespace std;

class Mutex;
class FileLog;
class GisusDB;
class Keyword;
class Watcher;
class User;
class SrvPerfWatcher;

class TaskManager
{
public:
	////////////////////////////////////////////////////////////////////////////
	///////////////ctors///////////////////////////////////
	TaskManager(const User* user, GisusDB* gisusdb, DMFun f = NULL, FileLog* filelog = NULL);
	~TaskManager();

	Task* GetReadyTask(void);
	const char* GetCaptcha(size_t& size, bool& urgent);
	bool SetCaptcha(const string& captcha);

	std::vector<Task*> GetYKTasks(void);
	bool IsBusy(bool countWCaptcha = false) const;

private:
	
	friend class Cordinator;
	friend class YKCordinator;

	void DisplayMsg(string msg, int msgType) const;
	void AddWCaptcha(Task* task, bool highPriority = false);
	void ToExecute(Task* task, bool highPriority = false);


	SafeQue<Task*> wCaptchas;
	SafeQue<Task*> wHCaptchas;
	SafeQue<Task*> readyTasks;
	SafeQue<Task*> readyHTasks;

	Task* curCaptchaTask;
	const User* user;
	GisusDB* gisusdb;
	DMFun displayCBFn;
	FileLog* _filelog;
};